package com.oa.modules.approvalsec1.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.oa.modules.approvalsec1.entity.LicenceEntity;
import com.oa.modules.approvalsec1.entity.StatuteInfoEntity;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.oa.modules.approvalsec1.entity.StatuteLibraryEntity;
import com.oa.modules.approvalsec1.service.StatuteLibraryService;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;



/**
 * 政策法规库
 *
 * @author jimboo
 * @email jimboo7339@163.com
 */
@RestController
@RequestMapping("statutelibrary")
@Api(tags = "政策法规库相关接口")
public class StatuteLibraryController extends AbstractController {
    private static final Logger log = LoggerFactory.getLogger(StatuteLibraryController.class);
    @Autowired
    private StatuteLibraryService statuteLibraryService;

    /**
     * 列表
     */
    @GetMapping ("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = statuteLibraryService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/getTree")
    @ApiOperation(value = "获取所有类别")
    public R getTree() {

        List<StatuteLibraryEntity> list = statuteLibraryService.getTree();

        return R.ok().put("data", list);
    }

    @GetMapping("/selectTree")
    @ApiOperation(value = "选择树结构")
    public R selectTree() {

        List<StatuteLibraryEntity> list = statuteLibraryService.selectTree();

        return R.ok().put("data", list);
    }

    @PostMapping("/getTextByIds")
    @ApiOperation(value = "通过ID数组获取法定依据")
    public R getTextByIds(@RequestBody Integer[] ids) {
        if(ids.length>0){
            log.info("ids-----" + Arrays.toString(ids));
            List<StatuteLibraryEntity> list = statuteLibraryService.getTextByIds(ids);
            //保存浏览记录
            statuteLibraryService.insertRecord(ids);

            return R.ok().put("data", list);
        }else {
            return R.ok().put("data", null);
        }

    }


    @PostMapping("/getChildrenByName")
    @ApiOperation(value = "通过名称获取具体子类（父类查询）")
    public R getChildrenByName(@RequestBody String name) {

        log.info("name-----" + name);
        List<StatuteLibraryEntity> list = statuteLibraryService.getChildrenByName(name);
        return R.ok().put("data", list);


    }


    @PostMapping("/getChildrenByName2")
    @ApiOperation(value = "通过名称获取具体子类（父子联查）")
    public R getChildrenByName2(@RequestBody String name) {

        log.info("name-----" + name);
        List<StatuteLibraryEntity> list = statuteLibraryService.getChildrenByName2(name);
        return R.ok().put("data", list);


    }


    @GetMapping("/getDataByCategory")
    @ApiOperation(value = "按类型浏览量")
    public R getDataByCategory(){
        List<Map> list = statuteLibraryService.getDataByCategory();

        return R.ok().put("data", list);
    }

    @GetMapping("/getDataByMonth")
    @ApiOperation(value = "按时间(月份)")
    public R getDataByMonth(){


        List<LinkedHashMap> list = statuteLibraryService.getDataByMonth();

        return R.ok().put("data", list);
    }

    @GetMapping("/getDataViews")
    @ApiOperation(value = "按浏览量(月、季)")
    public R getDataViews(){


        List<Map> list = statuteLibraryService.getDataViews();

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") Integer id){
		StatuteLibraryEntity statuteLibrary = statuteLibraryService.getInfoById(id);

        return R.ok().put("statuteLibrary", statuteLibrary);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody StatuteLibraryEntity statuteLibrary){
		statuteLibraryService.save(statuteLibrary);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody StatuteLibraryEntity statuteLibrary){
		statuteLibraryService.updateById(statuteLibrary);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody Integer[] ids){
		statuteLibraryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @PostMapping("/insertInfo")
    @ApiOperation(value = "保存政策法规相关信息")
    public R save(@RequestBody StatuteInfoEntity statuteinfo){
        //查询是否存在重复
        List<StatuteInfoEntity> list = statuteLibraryService.selectStatuteInfo(statuteinfo);
        if(list.size()>0){
            return R.ok();
        }else {
            statuteLibraryService.insertInfo(statuteinfo);
            return R.ok();
        }

    }

    @PostMapping("/getStatuteInfoTotal")
    @ApiOperation(value = "获取类别总数")
    public R getStatuteInfoTotal(@RequestBody Map<String, Object> params){

        log.info("查询参数" + params);
        List<Map> list = statuteLibraryService.getStatuteInfoTotal(params);

        return R.ok().put("data", list);
    }

    @PostMapping("/getStatuteInfoList")
    @ApiOperation(value = "获取list")
    public R getStatuteInfoList(@RequestBody Map<String, Object> params){

        log.info("查询参数" + params);
        List<StatuteInfoEntity> list = statuteLibraryService.getStatuteInfoList(params);

        return R.ok().put("data", list);
    }

}
